/**
 *
 */
package com.totyu.bpo.raffle.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.totyu.bpo.raffle.consts.RaffleConstants;
import com.totyu.bpo.raffle.domain.User;
import com.totyu.bpo.raffle.model.BingoMessage;
import com.totyu.bpo.raffle.model.YaohaoMessage;
import com.totyu.bpo.raffle.service.UserService;

/**
 * @author yuqy
 *
 */
@RestController
public class RafleController {

	@Autowired
	private UserService userService;

	@GetMapping(RaffleConstants.LIST)
	public List<User> list() {
		return userService.list();
	}

	@PostMapping(value = RaffleConstants.LOGIN, produces = "application/json;charset=UTF-8")
	public User login(@RequestBody User user) {
		if ("bpo2020".equalsIgnoreCase(user.getEmpCd())) {
			return new User(0L, "2020", "bpo2020", null, null);
		}
		return getUserByEmpCd(user);
	}

	@PostMapping(RaffleConstants.SUBMIT)
	public User submit(@RequestBody User user) throws Exception {
		User old = getUserByEmpCd(user);
		if (old != null && StringUtils.isEmpty(old.getTicketCard())) {
			int result = userService.updateTicketCard(user.getTicketCard(), old.getId());
			if (result > 0) {
				return new User(old.getId(), old.getEmpName(), StringUtils.upperCase(user.getEmpCd()),
						user.getTicketCard(), old.getWinningNumbers());
			}
		}
		return null;
	}

	@PostMapping(RaffleConstants.WM)
	public User submitWinningNumbers(@RequestBody User user) throws Exception {
		User old = getUserByEmpCd(user);
		if (old != null && !StringUtils.isEmpty(old.getTicketCard())) {
			int result = userService.updateWinningNumbers(user.getWinningNumbers(), old.getId());
			if (result > 0) {
				return new User(old.getId(), old.getEmpName(), StringUtils.upperCase(user.getEmpCd()),
						old.getTicketCard(), user.getWinningNumbers());
			}
		}
		return null;
	}

	@MessageMapping(RaffleConstants.RESTART_MAPPING)
	@SendTo(RaffleConstants.RESTART)
	public BingoMessage restart(YaohaoMessage message) throws Exception {
		Thread.sleep(1000);
		return new BingoMessage(message.getName());
	}

	@MessageMapping(RaffleConstants.ZZ_MAPPING)
	@SendTo(RaffleConstants.ZZ)
	public BingoMessage zz(YaohaoMessage message) throws Exception {
		Thread.sleep(1000);
		return new BingoMessage(message.getName());
	}

	@MessageMapping(RaffleConstants.YAOHAO_MAPPING)
	@SendTo(RaffleConstants.BINGO)
	public BingoMessage yh(YaohaoMessage message) throws Exception {
		Thread.sleep(1000);
		return new BingoMessage(message.getName());
	}

	private User getUserByEmpCd(User user) {
		List<User> users = userService.findByEmpNameOrEmpCd("", StringUtils.upperCase(user.getEmpCd()));

		if (users != null && users.size() == 1) {
			return users.get(0);
		}
		return null;

	}

}
